package com.zhiyou.servlet;

import com.zhiyou.model.Contract;
import com.zhiyou.model.House;
import com.zhiyou.service.HouseService;
import com.zhiyou.service.impl.ContractServiceImpl;
import com.zhiyou.service.ContractService;
import com.zhiyou.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class ContractServlet extends HttpServlet {
    private ContractService cS=new ContractServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //接收参数
        String pageNoStr = req.getParameter("pageNo");
        System.out.println("pageNoStr="+pageNoStr);
        int pageNo = Integer.parseInt(pageNoStr);//第pageNo页
        System.out.println("pageNo="+pageNo);

        //接受模糊查询关键字
        String keyword = req.getParameter("keyword");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        //创建HashMap对象封装数据
        HashMap<String, String> keywordMap = new HashMap<>();
        keywordMap.put("keyword",keyword);
        keywordMap.put("startTime",startTime);
        keywordMap.put("endTime",endTime);

        int total = cS.getContractNum(keywordMap);//记录信息的数量
        //创建分页工具对象
        PageUtil pageUtil = new PageUtil(pageNo, total);
        List<Contract> contracts =cS.getContract(pageNo,pageUtil.getPageSize(),keywordMap);
        //List<House> houses =new HouseDao().getHouse(pageNo,pageSize);
        req.getSession().setAttribute("keywordMap",keywordMap);
        req.getSession().setAttribute("contracts",contracts);
        req.getSession().setAttribute("pageUtil",pageUtil);
        resp.sendRedirect(req.getContextPath()+"/view/contract/list.jsp");
    }
}
